<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="hr-block">
        <el-form
          ref="formData"
          :model="formData"
          label-width="120px"
          class="formInfo"
        >
          <el-form-item label="部门:">
            <el-checkbox-group
              v-model="formData.deptID"
              @change="getListByParams"
            >
              <el-checkbox
                v-for="item in departments"
                :key="item.id"
                :label="item.id"
              >
                {{ item.name }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-form>
      </el-card>
      <!-- 考勤数据 -->
      <el-card class="hr-block">
        <!-- 考勤列表 -->
        <div
          style="
            width: 100%;
            position: relative;
            overflow-y: scroll;
            max-height: 450px;
          "
        >
          <el-table :data="list" border style="width: 100%" class="tableInfo">
            <!-- <el-table-column prop="index" label="序号" width="120">
              <template slot-scope="scope">
                {{ scope.$index + 1 }}
              </template>
            </el-table-column> -->
            <el-table-column prop="username" label="姓名" width="120" fixed />
            <el-table-column prop="workNumber" label="工号" width="120" />
            <el-table-column prop="departmentName" label="部门" width="120" />
            <el-table-column prop="mobile" label="手机" width="120" />
            <el-table-column
              v-for="(it, ind) in dayNumber"
              :key="ind"
              :label="`${currMonth}/${ind + 1}`"
              width="110"
            >
              <template slot-scope="scope">
                <span
                  class="atte"
                  @click="
                    showChangeDialog(
                      scope.row,
                      ind,
                      scope.row.attendanceRecord[ind]
                    )
                  "
                >
                  <span v-if="scope.row.attendanceRecord[ind].adtStatu === 0"
                    >--</span
                  >
                  <span
                    style="color: green"
                    v-if="scope.row.attendanceRecord[ind].adtStatu === 1"
                    >√</span
                  >
                  <span
                    style="color: red"
                    v-if="scope.row.attendanceRecord[ind].adtStatu === 2"
                    >旷工</span
                  >
                  <span
                    style="color: skyblue"
                    v-if="scope.row.attendanceRecord[ind].adtStatu === 3"
                    >请假</span
                  >
                  <span
                    style="color: black"
                    v-if="scope.row.attendanceRecord[ind].adtStatu === 4"
                    >加班</span
                  >
                  <span
                    style="color: grey"
                    v-if="scope.row.attendanceRecord[ind].adtStatu === 5"
                    >出差</span
                  >
                  <span
                    style="color: green"
                    v-if="scope.row.attendanceRecord[ind].adtStatu === 6"
                    >补签</span
                  >
                </span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <el-dialog :visible.sync="centerDialogVisible" width="30%" center>
          <span slot="title" style="color: #3f3f3f">
            {{ attendInfo.name }} {{ currMonth }}/{{ attendInfo.getDate }}
          </span>
          <div class="attenInfo">
            <p class="colRed">请谨慎操作</p>
            <p class="check">
              <el-radio-group v-model="modifyData.adtStatu">
                <el-radio
                  v-for="item in stateData"
                  :key="item.id"
                  :label="item.id"
                  >{{ item.value }}</el-radio
                >
              </el-radio-group>
            </p>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="btnOK">确定</el-button>
            <el-button @click="centerDialogVisible = false">取消</el-button>
          </span>
        </el-dialog>
        <!-- 分页组件 -->
        <!-- <el-row
          type="flex"
          align="middle"
          justify="center"
          style="height: 60px"
        >
          <el-pagination
            :page-size="page.pageSize"
            :current-page="page.page"
            layout="prev, pager, next"
            :total="page.total"
            @current-change="pageChange"
          />
        </el-row> -->
      </el-card>
    </div>
  </div>
</template>

<script>
import { getAttendancesList, updateAttendance } from '@/api/attendance'
import AttendanceSet from './components/attendance-set'
import { getDepartment } from '@/api/department'
import pageTools from './components/page-tools.vue'
export default {
  name: 'Attendances',
  components: { AttendanceSet, pageTools },
  data () {
    return {
      list: [],
      // MARK 重点变量
      currMonth: '',
      currYear: '',
      dayNumber: '',
      departmentChecks: '',
      selectData: [],
      stateData: [
        {
          id: '1',
          value: '正常',
          isEnable: false
        }, {
          id: '2',
          value: '旷工',
          isEnable: false
        }, {
          id: '3',
          value: '请假',
          isEnable: false
        }, {
          id: '4',
          value: '加班',
          isEnable: false
        }, {
          id: '5',
          value: '出差',
          isEnable: false
        }, {
          id: '6',
          value: '补签',
          isEnable: false
        }
      ],
      departments: [],
      total: 100,
      attendanceRecord: '',
      monthOfReport: '',
      centerDialogVisible: false,
      tipsDialogVisible: false,
      month: '',
      yearMonth: '',
      loading: false,
      attendInfo: {
        month: '',
        getDate: '',
        getInfo: '',
        name: '',
        counts: '',
        tobeTaskCount: ''
      },
      formData: {
        page: 1,
        pagesize: 100,
        keyword: this.keyword,
        deptID: [],
        stateID: ''
      },
      page: {
        page: 1,
        pageSize: 100
      },
      modifyData: {
        userId: '',
        day: '',
        adtStatu: ''
      }
    }
  },
  // 创建完毕状态
  created () {
    this.getAttendancesList([]) // 获取考勤列表
    this.getDepartment() // 获取部门列表
  },
  methods: {
    // 获取组织列表
    async getDepartment () {
      const data = await getDepartment()
      this.departments = data.slice(1);//类比 vc.erase(1);
      console.log(this.departments)
    },

    // 初始化数据
    async getAttendancesList () {
      // this.loading = true
      // 判断当前日
      // 获取当前日期的 day
      const now = new Date();
      const currentDay = now.getDate();
      console.log(currentDay)
      // for (let i = 0; i < currentDay; i++) {
      //   console.log(i);
      // }
      let { data } = await getAttendancesList({ ...this.page, departmentChecks: this.departmentChecks })
      // MARK 将已经过去的日子中无记录的设为旷工
      data.rows.forEach(e => {
        for (let i = 0; i < currentDay; i++) {
          // e.attendanceRecord[i].adtStatu = 5
          if (e.attendanceRecord[i].adtStatu == 0) {
            e.attendanceRecord[i].adtStatu = 2
          }
        }
      });
      this.list = data.rows // 当前记录

      console.log(data.rows)
      this.page.total = data.total // 总条数
      this.currMonth = data.currMonth
      this.currYear = data.currYear
      this.dayNumber = data.dayNumber
      // this.loading = false
    },
    // 确定修改
    async btnOK () {
      await updateAttendance(this.modifyData)
      this.$message.success('更新成功')
      this.centerDialogVisible = false
      this.getAttendancesList() // 成功之后 重新拉取数据
    },
    // 页码改变
    pageChange (page) {
      this.page.page = page
      this.getAttendancesList() // 获取数据
    },
    getListByParams (departmentChecks) {
      this.departmentChecks = departmentChecks.join(',') //数组转成 不带[]的字符串再赋值
      console.log(this.departmentChecks); // 多选框的id数组
      this.getAttendancesList()
    },
    showChangeDialog (item, id, it) {
      this.modifyData.id = it.id
      this.modifyData.day = it.day
      this.modifyData.adtStatu = it.adtStatu + ''// 这里要和statuType对应类型
      console.log(it.adtStatu)
      // 弹层数据
      if (it.adtStatu !== '') {
        this.attendInfo.getDate = it.day
        this.attendInfo.getInfo = it.adtStatu
        this.attendInfo.name = item.username
        this.centerDialogVisible = true
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.atte {
  cursor: pointer;
}
.atte:hover {
  background: skyblue;
}
.attenInfo {
  p {
    &.check {
      padding: 20px 0 0;
    }
    .el-radio {
      display: inline-block;
      width: 60px;
      padding: 5px 0;
    }
  }
}
</style>
